package com.hskn.hss.utils;

import cn.hutool.core.lang.tree.Tree;
import com.hskn.hss.entity.TreeEntity;
import java.util.ArrayList;
import java.util.List;

public class TreeUtil{
    // RecursiveEquipmentType
    public static List<TreeEntity> RecursiveAddress(List<TreeEntity> treeNodes){
        List<TreeEntity> trees = new ArrayList<TreeEntity>();
        for (TreeEntity treeNode : treeNodes) {
            if ("".equals(treeNode.getParentid()) || null==treeNode.getParentid() ) {
                trees.add(findAddressChildren(treeNode,treeNodes));
            }
        }
        return trees;
    }

    /**
     * 递归查找地址子节点
     * @param treeNodes
     * @return
     */
    public static TreeEntity findAddressChildren(TreeEntity treeNode,List<TreeEntity> treeNodes) {
        for (TreeEntity it : treeNodes) {
            if(treeNode.getId().equals(it.getParentid())) {
                if (treeNode.getChildren() == null) {
                    treeNode.setChildren(new ArrayList<TreeEntity>());
                }
                treeNode.getChildren().add(findAddressChildren(it,treeNodes));
            }
        }
        return treeNode;
    }

    /**
     * 递归查找父节点
     * @param treeNodes
     * @return
     */
    public static List<TreeEntity> findAddressFather(String id, List<TreeEntity> treeNodes){
        List<TreeEntity> outtree = new ArrayList<>();
        for (TreeEntity treeNode : treeNodes) {
            if(treeNode.getId().equals(id)){
                if(StringUtils.isNotEmpty(treeNode.getParentid())){
                    outtree.add(treeNode);
                    outtree.addAll(findAddressFather(treeNode.getParentid(),treeNodes));
                } else{
                    outtree.add(treeNode);
                }

            }
        }
        return outtree;
    }
}
